{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "19bf8baf-ea35-4fad-84a1-687eaf8e8b50",
   "metadata": {},
   "outputs": [],
   "source": [
    "nums=[1,8,2,23,7,-1,48,41,16,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9c70b32c-4f8c-4b68-9036-d3971c0e10b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import heapq\n",
    "heap=list(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "16fbda1c-c4cb-40f5-be03-7791335b09e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[-1, 2, 1, 16, 7, 2, 48, 41, 23, 8]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "heapq.heapify(heap)\n",
    "heap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7ad5dd5a-d3a3-45a9-a678-2aff33f91053",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import collections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0394f210-3e52-45c1-a56a-10d89d59e9c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import defaultdict\n"
   ]
  },
  {
   "cell_type": "raw",
   "id": "7f5f5987-d146-4bb7-8995-fa742a7f3014",
   "metadata": {},
   "source": [
    "from collections import defaultdict\n",
    "\n",
    "# 创建一个默认值为列表的 defaultdict\n",
    "d = defaultdict(list)\n",
    "\n",
    "# 往 'a' 键对应的列表中追加一个值\n",
    "d['a'].append(1)\n",
    "\n",
    "# 输出 defaultdict 的内容\n",
    "print(d) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "c393d4d5-3b1c-46fc-83ff-7ee429b157cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1, 2, 34, 4)]\n"
     ]
    }
   ],
   "source": [
    "print(list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "b6926cf6-91d0-42ad-9c03-4e0e66ef5a42",
   "metadata": {},
   "outputs": [],
   "source": [
    "del list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "77e4cb55-789a-4b10-9ca7-3b8d2f7d1c7f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "defaultdict(<class 'list'>, {'a': [1]})\n"
     ]
    }
   ],
   "source": [
    "from collections import defaultdict\n",
    "\n",
    "# 创建一个默认值为列表的 defaultdict\n",
    "d = defaultdict(list)\n",
    "\n",
    "# 往 'a' 键对应的列表中追加一个值\n",
    "d['a'].append(1)\n",
    "\n",
    "# 输出 defaultdict 的内容\n",
    "print(d) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "fa1994a4-ed83-4e64-b68b-10fb48a0c8b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "pairs=([1,2],[3,4],[1,3])\n",
    "for key,value in pairs:\n",
    "    d[key].append(value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "000bb704-0af3-4fb8-b543-265b5d6a6321",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "defaultdict(<class 'list'>, {'a': [1], 1: [2, 3], 3: [4]})\n"
     ]
    }
   ],
   "source": [
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "aafee73d-3d30-4cfb-af8c-8ec34a9012b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "product_b\n"
     ]
    }
   ],
   "source": [
    "prices = {\n",
    "    'product_a': 10,\n",
    "    'product_b': 5,\n",
    "    'product_c': 8\n",
    "}\n",
    "result = min(prices, key=lambda k: prices[k])\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "7105f774-e6b0-4136-8386-cdcfbda0cfbc",
   "metadata": {},
   "outputs": [],
   "source": [
    "person_list = [\n",
    "    {\n",
    "        \"姓\": \"c\",\n",
    "        \"名\": \"三\",\n",
    "        \"生日\": \"1990-05-10\"\n",
    "    },\n",
    "    {\n",
    "        \"姓\": \"b\",\n",
    "        \"名\": \"四\",\n",
    "        \"生日\": \"1992-08-15\"\n",
    "    },\n",
    "    {\n",
    "        \"姓\": \"aa\",\n",
    "        \"名\": \"五\",\n",
    "        \"生日\": \"1988-03-20\"\n",
    "    },\n",
    "    {\n",
    "        \"姓\": \"d\",\n",
    "        \"名\": \"六\",\n",
    "        \"生日\": \"1995-11-08\"\n",
    "    }\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "7d27aeda-5fb5-4506-a2a5-b86a27590605",
   "metadata": {},
   "outputs": [],
   "source": [
    "from operator import itemgetter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "745a139a-4069-4951-918e-4960ae57441d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'姓': 'aa', '名': '五', '生日': '1988-03-20'}, {'姓': 'b', '名': '四', '生日': '1992-08-15'}, {'姓': 'c', '名': '三', '生日': '1990-05-10'}, {'姓': 'd', '名': '六', '生日': '1995-11-08'}]\n"
     ]
    }
   ],
   "source": [
    "row_by_name=sorted(person_list,key=itemgetter('姓'))\n",
    "print(row_by_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "3a17983e-3f5b-43ea-a7ee-410cfeb0ac17",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1990-05-10\n",
      "  {'姓': 'c', '名': '三', '生日': '1990-05-10'}\n",
      "1992-08-15\n",
      "  {'姓': 'b', '名': '四', '生日': '1992-08-15'}\n",
      "1988-03-20\n",
      "  {'姓': 'aa', '名': '五', '生日': '1988-03-20'}\n",
      "1995-11-08\n",
      "  {'姓': 'd', '名': '六', '生日': '1995-11-08'}\n"
     ]
    }
   ],
   "source": [
    "from itertools import groupby\n",
    "for xing,ming in groupby(person_list,key=itemgetter('生日')):\n",
    "    print(xing)\n",
    "    for i in ming:\n",
    "        print(' ',i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01ed049e-4124-4b70-84c2-371c9dbe2e24",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
